package com.langshixiaobai.config;

import com.langshixiaobai.api.ApiTest;
import com.langshixiaobai.api.GameApi;
import com.langshixiaobai.api.MatchApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {

    @Autowired
    private ApiTest apiTest;

    @Autowired
    private MatchApi matchApi;

    @Autowired
    private GameApi gameApi;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(apiTest, "/test");

        registry.addHandler(matchApi, "/findMatch")
                //将Http session放到Websocket session中
                .addInterceptors(new HttpSessionHandshakeInterceptor());

        //同样将HttpSession添加到WebSocketSession中
        registry.addHandler(gameApi, "/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
